﻿using JshRCSWebApi.Infrastructure;
using JshRCSWebApi.Infrastructure.Domain.DTO.SysVehicle;
using JshRCSWebApi.Infrastructure.Domain.VO.SysVehicle.VehiclePush;
using JshRCSWebApi.Service.SysVehicle;
using JshRCSWebApi.Util.VehicleTool;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace JshRCSWebApi.ServiceImpl.SysVehicle
{
    public class VehiclePushServiceImpl: VehiclePushService
    {
        private int vehiclePort = 19301; 
        public async Task<ApiResult> robot_push_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.askJson = ""; 
            try
            { 
                    SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.RobotPushMessage(request.vehicleIP, request.vehiclePort);
                    return await Task.Run(() => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_push_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }

        }
    }
}
